// d:\project\honey-Alliance-project\shenye-dy-applet\pages\login\login_phone\bind_phone\bind_phone.js
import {
  dyBingPhone,
  dyBingPhoneSms
} from '../../../api/login'
const handleAuthorize = require('../../../utils/handleAuthorize')
const app = getApp()
Page({
  data: {
      codetip:'获取验证码', 
      smsCode: '',
      phone: '',
      linkJump:'',
      isabled:true, //获取验证码防止点击
  },
  onLoad: function (options) {
    if(!handleAuthorize.isEmpty(options.linkJump)){
      this.setData({
        linkJump:options.linkJump
      })
  }
  },
  // 暂不登录
  notCurrentlyBound(){
    tt.reLaunch({
      url:'/pages/index/index'
    })
  },
    // 获取短信
    getSmsCode(e){
      this.setData({
        smsCode: e.detail.value
      });
    },
    // 获取手机号码
    getPhone(e){
      this.setData({
        phone: e.detail.value
      });
    },
  // 点击获取验证码
  async getPhoneCode(){
    try {
        if (handleAuthorize.isEmpty(this.data.phone)) {
          tt.showToast({
            title:'请输入手机号码',
            icon:'fail'
          });
          return false;
        } 
        // 控制倒计时期间不可再点击
        if(this.data.isabled==false){
          return
        }
        const res = await dyBingPhoneSms(this.data.phone);
        tt.showToast({
          icon: 'none',
          title: '发送成功,请注意查看短信'
        })
        this.setData({
          isabled:false
        })
        let count=60
        let timer=setInterval(()=>{
          count--
          if(count==0){
            this.setData({
              codetip:'重新获取',
              isabled:true
            })
            clearInterval(timer)
          }else{
            this.setData({
              codetip:count+'s'
            })
          }
        },1000)
    } catch (error) {
      
    }
  },
  async getLoginDySms() {
    console.log(this.data)
    if (handleAuthorize.isEmpty(this.data.phone)) {
      tt.showToast({
        title:'请输入手机号码',
        icon:'fail'
      });
      return false;
    } else if (handleAuthorize.isEmpty(this.data.smsCode)) {
      tt.showToast({
        title:'请输入手机验证码',
        icon:'fail'
      });
      return false;
    } 
    var data = {
      phone: this.data.phone,
      code: this.data.smsCode,
    }
    // 开发appid 测试删除掉 
    data.appId = app.globalData.appId
    data.loginType = app.globalData.loginType

    tt.showLoading({
      title: '登录中'
    })
    try {
      const res = await dyBingPhone(data);
      // console.log('login_res',res)
      tt.hideLoading();
      tt.setStorageSync('isTemporary', false);
      tt.setStorageSync('token', res.result);
      tt.showToast({
        title: '登录成功',
        icon:'success'
      });
      // 判断是否有链接转跳
      if(!handleAuthorize.isEmpty(this.data.linkJump) && !handleAuthorize.isEmpty(tt.getStorageSync('linkJump'))){
        setTimeout(() => {
          var curRoute = tt.getStorageSync('linkJump')
          // 防止微信静默 登录url 出错
          if(curRoute.indexOf('/pages/login/login') > -1){
            curRoute = '/pages/index/index'
          }
          if(curRoute.indexOf('/pages/index/index') > -1 || curRoute.indexOf('/pages/my/my') > -1 || curRoute.indexOf('/pages/integralTask/integralTask')){
            tt.reLaunch({
              url: curRoute
            })
          }else{
            tt.redirectTo({
              url: curRoute
            })
          }
         
        }, 800)
      }else{ 
        setTimeout(() => {
          tt.reLaunch({
            url: '/pages/index/index'
          })
        }, 800)
      }
    } catch {
      
    } finally {
      
    }
  },
})